package com.bwjf.dzfp.common.base.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.bwjf.dzfp.common.util.DateUtil;

public class BaseAction {
	static Logger logger = Logger.getLogger(BaseAction.class);

	protected String getSessionOpenId(HttpServletRequest request) {
		return (String) request.getSession().getAttribute("openId");
	}

	protected PrintWriter getWriter(HttpServletResponse response) {
		PrintWriter p = null;
		try {
			p = response.getWriter();
		} catch (IOException e) {
			logger.error("response IO输入异常", e);
		}
		return p;
	}

	protected PrintWriter responsePrintJson(HttpServletResponse response,
			String json) {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter p = null;
		try {
			p = response.getWriter();
			p.print(json);
		} catch (IOException e) {
			logger.error("response IO输入异常", e);
		}

		return p;
	}

	protected PrintWriter responsePrint(HttpServletResponse response,
			String json, String contentType) {
		response.setContentType(contentType);
		PrintWriter p = null;
		try {
			p = response.getWriter();
		} catch (IOException e) {
			logger.error("response IO输入异常", e);
		}
		return p;
	}

	/**
	 * 自定义类型文件下载
	 */
	public HttpServletResponse download(String path,
			HttpServletResponse response, String downloadType) {
		try {
			File file = new File(path);
			FileInputStream fs = new FileInputStream(file);
			response.setHeader("content-type", downloadType);
			response.setHeader("content-disposition", "attachment;filename=\""
					+ URLEncoder.encode(file.getName(), "utf-8") + "\"");

			int b = 0;
			PrintWriter out = response.getWriter();
			while ((b = fs.read()) != -1) {
				out.write(b);
			}
			fs.close();
			out.close();
		} catch (IOException ex) {
			logger.error("文件下载异常：", ex);
		}
		return response;
	}

	/**
	 * 文件下载
	 */
	public HttpServletResponse download(String path,
			HttpServletResponse response) {
		try {
			File file = new File(path);
			FileInputStream fs = new FileInputStream(file);
			response.setHeader("content-type", "application/x-msdownload");
			response.setHeader("content-disposition", "attachment;filename=\""
					+ URLEncoder.encode(file.getName(), "utf-8") + "\"");
			int b = 0;
			PrintWriter out = response.getWriter();
			while ((b = fs.read()) != -1) {
				out.write(b);
			}
			fs.close();
			out.close();
		} catch (IOException ex) {
			logger.error("文件下载异常：", ex);
		}
		return response;
	}

	/**
	 * 文件下载
	 */
	public HttpServletResponse download(InputStream is, String filename,
			HttpServletResponse response) {
		try {
			response.setHeader("content-type", "application/x-msdownload");
			response.setHeader("content-disposition", "attachment;filename=\""
					+ URLEncoder.encode(filename, "utf-8") + "\"");

			int b = 0;
			PrintWriter out = response.getWriter();
			while ((b = is.read()) != -1) {
				out.write(b);
			}
			is.close();
			out.close();

		} catch (IOException ex) {
			logger.error("文件下载异常：", ex);
		}
		return response;
	}

	/**
	 * ========================================================================
	 * ============= upload file
	 * ================================================
	 * =====================================
	 */
	private List<File> files;

	public List<File> getFiles() {
		return files;
	}

	public void setFiles(List<File> files) {
		this.files = files;
	}

	private List<String> filesFileName;
	private List<String> filesContentType;

	public List<String> getFilesFileName() {
		return filesFileName;
	}

	public void setFilesFileName(List<String> filesFileName) {
		this.filesFileName = filesFileName;
	}

	public List<String> getFilesContentType() {
		return filesContentType;
	}

	public void setFilesContentType(List<String> filesContentType) {
		this.filesContentType = filesContentType;
	}

	// 上传
	public void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src), 102400);
				out = new BufferedOutputStream(new FileOutputStream(dst),
						102400);
				byte[] buffer = new byte[102400];
				int i = 0;
				while (i != -1) {
					i = in.read(buffer);
					if (i != -1) {
						out.write(buffer, 0, i);
					}
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			logger.error("接收上传文件异常：", e);
		}
	}

	/**
	 * 获取物理路径
	 * 
	 * @param name
	 * @return
	 */
	public String getRealPath(String name, HttpServletRequest request) {
		return request.getServletContext().getRealPath(name);
	}

	/**
	 * 获取post请求正文
	 * 
	 * @param br
	 * @return
	 * @throws IOException
	 */
	public static String getBodyString(HttpServletRequest request) {
		BufferedReader br = null;
		String inputLine;
		String str = "";
		try {
			br = request.getReader();
			while ((inputLine = br.readLine()) != null) {
				str += inputLine;
			}
			br.close();
		} catch (IOException e) {
			logger.error("IOException:正文解析失败！ ", e);
		}
		return str;
	}

	// 获取未加密11位序列号
	public String getSjs_11() {
		String formatDate = DateUtil.formatDateForId(new Date());
		StringBuffer sb = new StringBuffer(formatDate);
		Random r = new Random();
		for (int i = 0; i < 11; i++) {// 11位随机数
			sb.append(r.nextInt(9));
		}
		return sb.toString();
	}

}
